package com.biyabi.service.impl;

import com.biyabi.dao.CatelogMapper;
import com.biyabi.entity.Catelog;
import com.biyabi.entity.CatelogExample;
import com.biyabi.pojo.CatelogData;
import com.biyabi.pojo.CatelogsData;
import com.biyabi.service.CatelogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class CatelogServiceImpl implements CatelogService {

    @Resource
    private CatelogMapper catelogMapper;

    @Override
    public void insert(Catelog catelog, MultipartFile addCatelogLogoFile, HttpServletRequest request) {
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/upload/logo");
            catelog.setId(UUID.randomUUID().toString());
            catelog.setIslast(1);
            catelog.setPicture("upload/logo/"+addCatelogLogoFile.getOriginalFilename());
            catelogMapper.insert(catelog);
            Catelog catelog1 = new Catelog();
            catelog1.setId(catelog.getParentid());
            catelog1.setIslast(0);
            catelogMapper.updateByPrimaryKeySelective(catelog1);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    @Override
    public void update(Catelog catelog, MultipartFile updateCatelogLogoFile, HttpServletRequest request) {
        try {
            if(!updateCatelogLogoFile.isEmpty()){
                String realPath = request.getSession().getServletContext().getRealPath("/upload/logo");
                Catelog catelog1 = catelogMapper.selectByPrimaryKey(catelog.getId());
                String logoRealPath = request.getSession().getServletContext().getRealPath(catelog1.getPicture());
                File file = new File(logoRealPath);
                if(file.exists()){
                    file.delete();
                }
                catelog.setPicture("upload/logo/"+updateCatelogLogoFile.getOriginalFilename());
            }
            Catelog catelog1 = new Catelog();
            catelog1.setId(catelog.getParentid());
            catelog1.setIslast(0);
            catelogMapper.updateByPrimaryKeySelective(catelog1);


            Catelog catelog3 = catelogMapper.selectByPrimaryKey(catelog.getId());


            catelog.setIslast(catelogMapper.selectByPrimaryKey(catelog.getId()).getIslast());
            catelogMapper.updateByPrimaryKey(catelog);

            CatelogExample catelogExample = new CatelogExample();
            catelogExample.createCriteria().andParentidEqualTo(catelog3.getParentid());
            List<Catelog> catelogs = catelogMapper.selectByExample(catelogExample);
            if(catelogs.size()==0){
                Catelog catelog2 = new Catelog();
                catelog2.setId(catelog3.getParentid());
                catelog2.setIslast(1);
                catelogMapper.updateByPrimaryKeySelective(catelog2);
            }

        } catch (Exception e) {
            throw new RuntimeException();
        }
    }


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public CatelogsData findAll(Integer currentPage, Integer pageSize, String columnName, String columnValue) {
        currentPage = currentPage == null ? 1:currentPage;
        pageSize= pageSize== null ? 20:pageSize;
        CatelogExample catelogExample = new CatelogExample();
        if(columnName!=null && columnValue!=null){
            switch (columnName){
                case "name":
                    catelogExample.createCriteria().andNameLike("%"+columnValue+"%");
                    break;
            }
        }
        CatelogsData catelogsData = new CatelogsData();
        List<Catelog> catelogs = catelogMapper.selectByExample(catelogExample);
        ArrayList<CatelogData> catelogData1 = new ArrayList<>();
        for (Catelog catelog : catelogs) {
            if(catelog.getParentid().isEmpty()){
                CatelogData catelogData = new CatelogData();
                catelogData.setId(catelog.getId());
                catelogData.setText(catelog.getName());
                catelogData.setLevel(catelog.getLevel());
                catelogData.setIsLast(catelog.getIslast());
                catelogData.setPicture(catelog.getPicture());
                catelogData.setChildren(getChildren(catelog));
                catelogData1.add(catelogData);
            }
        }
        catelogsData.setRows(new ArrayList<CatelogData>());
        int start = (currentPage-1)*pageSize;
        int end = Math.min(currentPage*pageSize-1,catelogData1.size()-1);
        for (int i = start;i<=end;i++){
            catelogsData.getRows().add(catelogData1.get(i));
        }
        catelogsData .setTotal(catelogData1.size());
        return catelogsData;
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<CatelogData> findAllCatelog() {
        List<Catelog> catelogs = catelogMapper.selectByExample(new CatelogExample());
        ArrayList<CatelogData> catelogData1 = new ArrayList<>();
        CatelogData e = new CatelogData();
        e.setId("");
        e.setText("一级分类");
        catelogData1.add(e);
        for (Catelog catelog : catelogs) {
            if(catelog.getParentid().isEmpty()){
                CatelogData catelogData = new CatelogData();
                catelogData.setId(catelog.getId());
                catelogData.setText(catelog.getName());
                catelogData.setLevel(catelog.getLevel());
                catelogData.setIsLast(catelog.getIslast());
                catelogData.setPicture(catelog.getPicture());
                catelogData.setChildren(getChildren(catelog));
                catelogData1.add(catelogData);
            }
        }
        return catelogData1;
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Catelog> findAllLastCatelog() {
        CatelogExample example = new CatelogExample();
        example.createCriteria().andIslastEqualTo(1);
        return catelogMapper.selectByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<CatelogData> findAllCatelogWithoutMyself(String id) {
        List<Catelog> catelogs = catelogMapper.selectByExample(new CatelogExample());
        ArrayList<CatelogData> catelogData1 = new ArrayList<>();
        CatelogData e = new CatelogData();
        e.setId("");
        e.setText("一级分类");
        catelogData1.add(e);
        for (Catelog catelog : catelogs) {
            if(catelog.getParentid().isEmpty() && !catelog.getId().equals(id)){
                CatelogData catelogData = new CatelogData();
                catelogData.setId(catelog.getId());
                catelogData.setText(catelog.getName());
                catelogData.setLevel(catelog.getLevel());
                catelogData.setIsLast(catelog.getIslast());
                catelogData.setPicture(catelog.getPicture());
                catelogData.setChildren(getChildren(catelog,id));
                catelogData1.add(catelogData);
            }
        }
        return catelogData1;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Catelog findOne(String id) {
        return catelogMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Catelog> findAllLevleOfFirst() {
        CatelogExample example = new CatelogExample();
        example.createCriteria().andParentidIsNull();
        return catelogMapper.selectByExample(example);
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public  List<CatelogData> getChildren(Catelog c){
        ArrayList<CatelogData> catelogDatas = new ArrayList<>();
        if(c.getIslast()==1){
            return catelogDatas;
        }else{
            CatelogExample catelogExample = new CatelogExample();
            catelogExample.createCriteria().andParentidEqualTo(c.getId());
            List<Catelog> catelogs = catelogMapper.selectByExample(catelogExample);
            for (Catelog catelog : catelogs) {
                CatelogData catelogData = new CatelogData();
                catelogData.setId(catelog.getId());
                catelogData.setText(catelog.getName());
                catelogData.setLevel(catelog.getLevel());
                catelogData.setIsLast(catelog.getIslast());
                catelogData.setPicture(catelog.getPicture());
                catelogData.setChildren(getChildren(catelog));
                catelogDatas.add(catelogData);
            }
        }
        return catelogDatas;
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public  List<CatelogData> getChildren(Catelog c,String id){
        ArrayList<CatelogData> catelogDatas = new ArrayList<>();
        if(c.getIslast()==1){
            return catelogDatas;
        }else{
            CatelogExample catelogExample = new CatelogExample();
            catelogExample.createCriteria().andParentidEqualTo(c.getId());
            List<Catelog> catelogs = catelogMapper.selectByExample(catelogExample);
            for (Catelog catelog : catelogs) {
                if(!catelog.getId().equals(id)){
                    CatelogData catelogData = new CatelogData();
                    catelogData.setId(catelog.getId());
                    catelogData.setText(catelog.getName());
                    catelogData.setLevel(catelog.getLevel());
                    catelogData.setIsLast(catelog.getIslast());
                    catelogData.setPicture(catelog.getPicture());
                    catelogData.setChildren(getChildren(catelog));
                    catelogDatas.add(catelogData);
                }
            }
        }
        return catelogDatas;
    }
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Catelog> getLastChildrens(String catelogId){
        List<Catelog> list = new ArrayList<Catelog>();
        Catelog c = catelogMapper.selectByPrimaryKey(catelogId);
        if(0 == c.getIslast()){
            CatelogExample example = new CatelogExample();
            example.createCriteria().andParentidEqualTo(catelogId);
            List<Catelog> childrens = catelogMapper.selectByExample(example);
            for(Catelog catelog:childrens){
                if(0 == catelog.getIslast()){
                    list.addAll(getLastChildrens(catelog.getId()));
                }else{
                    list.add(catelog);
                }
            }
        }else{
            list.add(c);
        }
        return list;
    }

}
